package com.haven.webrobotserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.haven.webrobotserver.domain.ResultTestcaseTestdata;
import com.haven.webrobotserver.domain.ResultTestplanBatch;
import com.haven.webrobotserver.domain.Testplan;
import com.haven.webrobotserver.service.ExecuteService;
import com.haven.webrobotserver.service.ResultTestcaseTestdataService;
import com.haven.webrobotserver.service.ResultTestplanBatchService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/executeTest")
@CrossOrigin("http://localhost:8080")
public class ExecuteTestController {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ResultTestplanBatchService resultTestplanBatchService;

    @Autowired
    private ExecuteService executeServive;


    @PostMapping("/execute")
    public JSONObject execute(@RequestBody Map<String,String> params){
        JSONObject result = new JSONObject();
        /*封装resultTestplanBatch*/
        ResultTestplanBatch resultTestplanBatch = new ResultTestplanBatch();
        resultTestplanBatch.setTestplanId(Integer.parseInt(params.get("testplanId")));
        resultTestplanBatch.setDescription(params.get("description"));

        resultTestplanBatchService.insertOne(resultTestplanBatch);



        if(resultTestplanBatch.getId() == 0){
            result.put("status","500");
            return result;
        }

        Testplan testplan = new Testplan();
        testplan.setId(Integer.parseInt(params.get("testplanId")));
        String endRunnerIp = params.get("endRunnerIp");
        /*将任务交给任务执行service去执行*/
        executeServive.executeTest(testplan,resultTestplanBatch,endRunnerIp);
        result.put("status",200);
        return result;
    }

}
